/*********************  企业信息设置  **************************/
	var _common = xbdunRequire("common");
	var _data = null;
	
	$(function() {
		 $('#from_100101104').bootstrapValidator({
		    	message: 'This value is not valid',
	            live: 'enabled',
	            excluded:[":disabled"],
	            trigger: 'live blur',
		        feedbackIcons: {
		        	valid: 'glyphicon glyphicon-ok-sign',
	                invalid: 'glyphicon glyphicon-remove-sign',
	                validating: 'glyphicon glyphicon-refresh-sign'
		        },
		        fields: {
		            addr: {
		            	container: '#addr_info',
		                validators: {
		                    notEmpty: {
		                        message: '地址不能为空'
		                    }
		                }
		            },
		            website: {
		            	container: '#website_info',
		            	validators: {
		            		notEmpty: {
		            			message: '企业网站不能为空'
		            		},
				            regexp: {
				            	regexp: /^((https|http|ftp|rtsp|mms):\/\/)?[^s]+/,
				            	message: '网址有误 例:http://www.xbdun.com'
				            }
		            	}
		            },
		            orgEmail: {
		            	container: '#orgEmail_info',
		                validators: {
		                	notEmpty: {
		            			message: '企业邮箱不能为空'
		            		},
		                    emailAddress: {
		                        message: '邮箱格式不正确'
		                    }
		                }
		            },
		            orgLandline: {
		            	container: '#orgLandline_info',
		                validators: {
		                	regexp: {
				            	regexp: /[0-9]{3,4}\-[0-9]{6}/,
				            	message: '格式有误,例:023-60661234'
				            }
		                }
		            },
		            orgMobile: {
		            	container: '#orgMobile_info',
		            	validators: {
		            		notEmpty: {
		            			message: '企业电话不能为空'
		            		}
		            	}
		            }
		        }
		    });
		    
		    $("#btn_100101104100").on("click",function(){
		    	if(!$("#txt_logo").val()){
	    			return _common.alert(true,"请上传企业Logo");
	    		}
		    	var result = null;
		    	var _orgFileds = {};
		    	$("#tab_content4 input[id^='orgFiled_'],textarea[id^='orgFiled_']").each(function(){
		    		_orgFileds[$(this).attr("name")] = $(this).val()
		    	})
		    	$.post(webRoot+"/system/100101104100X",_orgFileds,function(reslt){
					setTimeout(function(){
						if(reslt){
							if(!reslt.success){
								result = reslt.subMsg;
							}
						}
					},1000)
				})
		    	
		    	var bootstrapValidator = $("#from_100101104").data('bootstrapValidator');
		    	bootstrapValidator.validate();
		    	if(bootstrapValidator.isValid()){
		    		var tempJSon = {};
		    		var _k = ["addr","orgLandline","orgMobile","website","logo"];
					$.each(_k,function(i,n){
						if(_data[n]!=$("#txt_"+n).val()){
							tempJSon[n]=$("#txt_"+n).val();
							_data[n]=$("#txt_"+n).val();
						}
					})
					if(!$.isEmptyObject(tempJSon)){
						_common.loading(true,"正在保存中...");
						tempJSon["Id"]=_data.id;
				    	$.post(webRoot+"/system/100101104100",tempJSon,function(reslt){
							setTimeout(function(){
								_common.loading(false);
								if(reslt){
									if(reslt.success&&!result){
										_common.alert(true,'修改成功');
									}else{
										_common.alert(true,'修改失败,原因是'+reslt.subMsg+","+result);
									}
								}
							},1000)
						})
					}else{
						if(result){
							_common.alert(true,"修改企业简介出错,原因是"+result);
						}else{
							_common.alert(true,"企业简介修改成功");
						}
					}
		    	}else return;
		    })
	});
	
	/**
	 * 加载数据
	 */
	setTimeout(function(){
		$.post(webRoot+"/system/100101104X",null,function(reslt){
			if(reslt){
				if(reslt.success){
					_data = reslt.data;
					if(_data){
						for(var _key in _data){
							var _object = $("#txt_"+_key+",#sel_"+_key);
							if(_object.size()>0){
								if(_data[_key]){
									_object.val(_data[_key]);
									if(_key!="addr"&&_key!="orgLandline"&&_key!="orgMobile"&&_key!="website"){
										_object.attr("disabled","disabled");
									}
								}
							}
						}
						//加载图片
						setTimeout(function(){
							$.post(webRoot+"/system/100101104XX",{logo:_data.logo,recordImg:_data.recordImg},function(result){
								if(result){
									if(result.success){
										var _d = JSON.parse(result.data);
										if(_d.recordImg){
											$("#targetId2").attr("src",_d.recordImg.replace(/\\/g,"/"));
										}else{
											$("#targetId2").attr("title", "加载失败");
										}
										if(_d.logo){
											$("#targetId").attr("src",_d.logo.replace(/\\/g,"/"));
										}else{
											$("#targetId").attr("title", "加载失败");
										}
									}else{
										_common.alert(true,result.subMsg);
									}
								}
							})
						},500)
					}
				}else{
					_common.alert(true,reslt.subMsg);
				}
			}
		})
	},50)
	
	var isLoad = false;
	//选中企业附属信息，加载信息
	$("#profile-tab4").on("click",function(){
		if(isLoad){
			return;
		}
		_common.loading(true,"正在加载中...");
		$.post(webRoot+"/system/100101104XXX",null,function(result){
			setTimeout(function(){
				_common.loading(false);
				if(result){
					if(result.success){
						isLoad = true;
						if(result.data){
							var res = result.data;
							for(var _key in res){
								var _object = $("#orgFiled_"+_key);
								if(_object.size()>0&&res[_key]){
									_object.val(res[_key]);
								}
							}
						}
					}else{
						_common.alert(true,result.subMsg);
					}
				}
			},1000)
		})
	})
